package br.ufrj.ppgi.jemf_advanced_example.emergency;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.Emergency;
import br.ufrj.ppgi.jemf.mobile.view.EmergencyView;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single Emergency detail screen.
 * This fragment is either contained in a {@link EmergencyListActivity} in two-pane mode (on tablets) or a {@link EmergencyDetailActivity} on handsets.
 */
public class EmergencyDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_EMERGENCY = "activated_emergency";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "EMERGENCY";
    /**
     * The emergency content this fragment is showing.
     */
    private static Emergency emergency;
    /**
     * The emergency view holder.
     */
    private static EmergencyView emergencyView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public EmergencyDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the emergency content specified by the fragment arguments.
        if (getArguments().containsKey(EmergencyDetailFragment.ARG_ID)) {
            emergency = getArguments().getParcelable(EmergencyDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the emergency content
	    if (emergency != null) {
	    	emergencyView = new EmergencyView(getActivity(), false);
	    	// Name
	    	emergencyView.setEmergencyNameText(emergency.getName());
	    	// Activated
	    	emergencyView.setEmergencyActivatedBool(emergency.getActivated());
	    	// Level
	    	emergencyView.setEmergencyLevelText(emergency.getLevelString());
	    	// Type
	    	emergencyView.setEmergencyTypeText(emergency.getTypeString());
	    	// Start Date
	    	emergencyView.setEmergencyStartDateText(emergency.getStartDateString());
	    	// End Date
	    	emergencyView.setEmergencyEndDateText(emergency.getEndDateString());

	    	// Its necessary to add the EmergencyView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add Emergency View as a child of the ScrollView.
	    	scrollView.addView(emergencyView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated emergency
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_EMERGENCY)) {
        	emergency = savedInstanceState.getParcelable(STATE_EMERGENCY);
        }
	}





	/**
     * Get updated Emergency
     * @return Emergency
     */
	public static Emergency getEmergency() {
		if (BuildConfig.DEBUG) Log.i(EmergencyListActivity.TAG, "EmergencyDetailFragment ::: getEmergency");
    	return emergency;
    }

	/**
	 * Update Emergency data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((emergency != null) && (emergencyView != null)) {
    		if (BuildConfig.DEBUG) Log.i(EmergencyListActivity.TAG, "EmergencyDetailFragment ::: updateDataFromUI");
    		// Update Emergency object with new data included by user through components
    		emergency.setName(emergencyView.getEmergencyNameText());
	    	emergency.setActivated(emergencyView.getEmergencyActivatedBool());
    		emergency.setLevelString(emergencyView.getEmergencyLevelText());
    		emergency.setTypeString(emergencyView.getEmergencyTypeText());
    		emergency.setStartDateString(emergencyView.getEmergencyStartDateText());
    		emergency.setEndDateString(emergencyView.getEmergencyEndDateText());
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated emergency.
        outState.putParcelable(STATE_EMERGENCY, emergency);
    }
}
